{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!pip3 install sklearn_pandas==2.0.4\n",
    "!pip3 install catboost==0.24.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from catboost import CatBoostClassifier\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import OrdinalEncoder, StandardScaler, OneHotEncoder\n",
    "from sklearn_pandas import DataFrameMapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "categorical_features = ['feat_5', 'feat_6', 'feat_7', 'feat_8']\n",
    "numerical_features = ['feat_1', 'feat_2', 'feat_3', 'feat_4']\n",
    "\n",
    "X, y = make_classification(n_samples=10000, \n",
    "                           n_features=4, \n",
    "                           n_redundant=0, \n",
    "                           random_state=42, \n",
    "                           weights=[0.5])\n",
    "\n",
    "# Add categorical columns\n",
    "for col in range(4):\n",
    "    num_classes = np.random.randint(2, 10)\n",
    "    cat_col = np.random.randint(num_classes, size=X.shape[0]).reshape(-1,1)\n",
    "    X = np.hstack((X, cat_col))\n",
    "\n",
    "# To DataFrame\n",
    "columns = [f'feat_{i+1}' for i in range(X.shape[1])]\n",
    "X = pd.DataFrame(X, columns=columns)\n",
    "y = pd.DataFrame(y, columns=['label'])\n",
    "\n",
    "# Scale regressors, modify categoricals\n",
    "for col in numerical_features:\n",
    "    mean = np.random.randint(10, 1000)\n",
    "    std = np.random.randint(1, 100)\n",
    "    X[col] = X[col].apply(lambda x: mean + std * x).astype(int)\n",
    "\n",
    "for col in categorical_features:\n",
    "    X[col] = X[col].apply(lambda x: f'str_{x}' if np.isnan(x)==False else x)\n",
    "\n",
    "# Create Nans in dataset\n",
    "for col in categorical_features + numerical_features:\n",
    "    X[col] = X[col].sample(frac=0.7)\n",
    "    \n",
    "df = X.merge(y,left_index=True, right_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feat_1</th>\n",
       "      <th>feat_2</th>\n",
       "      <th>feat_3</th>\n",
       "      <th>feat_4</th>\n",
       "      <th>feat_5</th>\n",
       "      <th>feat_6</th>\n",
       "      <th>feat_7</th>\n",
       "      <th>feat_8</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8886</th>\n",
       "      <td>446.0</td>\n",
       "      <td>565.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_6.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1514</th>\n",
       "      <td>473.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>683.0</td>\n",
       "      <td>898.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5296</th>\n",
       "      <td>NaN</td>\n",
       "      <td>551.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>807.0</td>\n",
       "      <td>str_7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      feat_1  feat_2  feat_3  feat_4   feat_5   feat_6   feat_7   feat_8  \\\n",
       "8886   446.0   565.0     NaN     NaN  str_6.0  str_1.0  str_0.0  str_0.0   \n",
       "1514   473.0   549.0   683.0   898.0      NaN  str_0.0  str_0.0  str_0.0   \n",
       "5296     NaN   551.0     NaN   807.0  str_7.0      NaN  str_0.0  str_1.0   \n",
       "\n",
       "      label  \n",
       "8886      1  \n",
       "1514      1  \n",
       "5296      1  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sample(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df, test_df = train_test_split(df, test_size=0.1, shuffle=False)\n",
    "X_train, y_train = train_df[categorical_features + numerical_features], train_df['label']\n",
    "X_test, y_test = test_df[categorical_features + numerical_features], test_df['label']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preprocessing + Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 0.6863287\ttotal: 58.1ms\tremaining: 58s\n",
      "100:\tlearn: 0.4325719\ttotal: 558ms\tremaining: 4.96s\n",
      "200:\tlearn: 0.3935335\ttotal: 1.09s\tremaining: 4.32s\n",
      "300:\tlearn: 0.3793807\ttotal: 2.17s\tremaining: 5.04s\n",
      "400:\tlearn: 0.3720991\ttotal: 2.7s\tremaining: 4.04s\n",
      "500:\tlearn: 0.3667096\ttotal: 3.19s\tremaining: 3.18s\n",
      "600:\tlearn: 0.3618758\ttotal: 3.69s\tremaining: 2.45s\n",
      "700:\tlearn: 0.3577696\ttotal: 4.18s\tremaining: 1.78s\n",
      "800:\tlearn: 0.3536301\ttotal: 4.66s\tremaining: 1.16s\n",
      "900:\tlearn: 0.3500026\ttotal: 5.15s\tremaining: 566ms\n",
      "999:\tlearn: 0.3457159\ttotal: 5.89s\tremaining: 0us\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Pipeline(steps=[('preprocess',\n",
       "                 DataFrameMapper(df_out=True, drop_cols=[],\n",
       "                                 features=[(['feat_1'], [SimpleImputer()]),\n",
       "                                           (['feat_2'], [SimpleImputer()]),\n",
       "                                           (['feat_3'], [SimpleImputer()]),\n",
       "                                           (['feat_4'], [SimpleImputer()]),\n",
       "                                           (['feat_5'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OrdinalEncoder()]),\n",
       "                                           (['feat_6'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OrdinalEncoder()]),\n",
       "                                           (['feat_7'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OrdinalEncoder()]),\n",
       "                                           (['feat_8'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OrdinalEncoder()])])),\n",
       "                ('clf',\n",
       "                 <catboost.core.CatBoostClassifier object at 0x11bf004d0>)])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat = [([c], [SimpleImputer(strategy='constant', fill_value='UNK'),\n",
    "              OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1)]) for c in categorical_features]\n",
    "num = [([n], [SimpleImputer()]) for n in numerical_features]\n",
    "mapper = DataFrameMapper(num + cat, df_out=True)\n",
    "clf = CatBoostClassifier(iterations=1000,\n",
    "                         learning_rate=0.01,\n",
    "                         metric_period=100)\n",
    "\n",
    "pipeline = Pipeline([\n",
    "    ('preprocess', mapper),\n",
    "    ('clf', clf)\n",
    "])\n",
    "\n",
    "pipeline.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "preprocessed_X_test = mapper.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feat_1</th>\n",
       "      <th>feat_2</th>\n",
       "      <th>feat_3</th>\n",
       "      <th>feat_4</th>\n",
       "      <th>feat_5</th>\n",
       "      <th>feat_6</th>\n",
       "      <th>feat_7</th>\n",
       "      <th>feat_8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>9000</th>\n",
       "      <td>629.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9001</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>246.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_2.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9002</th>\n",
       "      <td>795.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>434.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9003</th>\n",
       "      <td>731.0</td>\n",
       "      <td>969.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_5.0</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9004</th>\n",
       "      <td>526.0</td>\n",
       "      <td>1009.0</td>\n",
       "      <td>439.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      feat_1  feat_2  feat_3  feat_4   feat_5   feat_6   feat_7   feat_8\n",
       "9000   629.0     NaN     NaN     4.0      NaN  str_0.0      NaN  str_1.0\n",
       "9001     NaN     NaN     NaN   246.0  str_1.0  str_2.0  str_1.0  str_4.0\n",
       "9002   795.0     NaN   434.0     NaN  str_1.0      NaN  str_1.0  str_5.0\n",
       "9003   731.0   969.0     NaN    -7.0  str_1.0  str_5.0  str_0.0      NaN\n",
       "9004   526.0  1009.0   439.0     NaN      NaN  str_1.0      NaN  str_2.0"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test[numerical_features + categorical_features].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feat_1</th>\n",
       "      <th>feat_2</th>\n",
       "      <th>feat_3</th>\n",
       "      <th>feat_4</th>\n",
       "      <th>feat_5</th>\n",
       "      <th>feat_6</th>\n",
       "      <th>feat_7</th>\n",
       "      <th>feat_8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>9000</th>\n",
       "      <td>629.000000</td>\n",
       "      <td>984.340446</td>\n",
       "      <td>452.364098</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9001</th>\n",
       "      <td>636.122757</td>\n",
       "      <td>984.340446</td>\n",
       "      <td>452.364098</td>\n",
       "      <td>246.000000</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9002</th>\n",
       "      <td>795.000000</td>\n",
       "      <td>984.340446</td>\n",
       "      <td>434.000000</td>\n",
       "      <td>75.207028</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9003</th>\n",
       "      <td>731.000000</td>\n",
       "      <td>969.000000</td>\n",
       "      <td>452.364098</td>\n",
       "      <td>-7.000000</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9004</th>\n",
       "      <td>526.000000</td>\n",
       "      <td>1009.000000</td>\n",
       "      <td>439.000000</td>\n",
       "      <td>75.207028</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          feat_1       feat_2      feat_3      feat_4  feat_5  feat_6  feat_7  \\\n",
       "9000  629.000000   984.340446  452.364098    4.000000     0.0     1.0     0.0   \n",
       "9001  636.122757   984.340446  452.364098  246.000000     2.0     3.0     2.0   \n",
       "9002  795.000000   984.340446  434.000000   75.207028     2.0     0.0     2.0   \n",
       "9003  731.000000   969.000000  452.364098   -7.000000     2.0     6.0     1.0   \n",
       "9004  526.000000  1009.000000  439.000000   75.207028     0.0     2.0     0.0   \n",
       "\n",
       "      feat_8  \n",
       "9000     2.0  \n",
       "9001     5.0  \n",
       "9002     6.0  \n",
       "9003     0.0  \n",
       "9004     3.0  "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocessed_X_test[numerical_features + categorical_features].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import dump, load\n",
    "dump(pipeline, 'params/pipeline.joblib')\n",
    "test_df.to_csv('params/test_df.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluation(pipeline, X, y):\n",
    "    y_predict_proba = pipeline.predict_proba(X)[:, 1]\n",
    "    return{\n",
    "        'auc': roc_auc_score(y, y_predict_proba)\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'auc': 0.9311860182931898}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluation(pipeline, X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'auc': 0.899970342583241}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluation(pipeline, X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Alternative"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pipeline(steps=[('preprocess',\n",
       "                 DataFrameMapper(df_out=True, drop_cols=[],\n",
       "                                 features=[(['feat_1'],\n",
       "                                            [SimpleImputer(),\n",
       "                                             StandardScaler()]),\n",
       "                                           (['feat_2'],\n",
       "                                            [SimpleImputer(),\n",
       "                                             StandardScaler()]),\n",
       "                                           (['feat_3'],\n",
       "                                            [SimpleImputer(),\n",
       "                                             StandardScaler()]),\n",
       "                                           (['feat_4'],\n",
       "                                            [SimpleImputer(),\n",
       "                                             StandardScaler()]),\n",
       "                                           (['feat_5'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OneHotEncoder()]),\n",
       "                                           (['feat_6'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OneHotEncoder()]),\n",
       "                                           (['feat_7'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OneHotEncoder()]),\n",
       "                                           (['feat_8'],\n",
       "                                            [SimpleImputer(fill_value='UNK',\n",
       "                                                           strategy='constant'),\n",
       "                                             OneHotEncoder()])])),\n",
       "                ('clf', LogisticRegression())])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat = [([c], [SimpleImputer(strategy='constant', fill_value='UNK'),\n",
    "              OneHotEncoder()]) for c in categorical_features]\n",
    "num = [([n], [SimpleImputer(), StandardScaler()]) for n in numerical_features]\n",
    "mapper = DataFrameMapper(num + cat, df_out=True)\n",
    "clf = LogisticRegression()\n",
    "\n",
    "pipeline = Pipeline([\n",
    "    ('preprocess', mapper),\n",
    "    ('clf', clf)\n",
    "])\n",
    "\n",
    "pipeline.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "preprocessed_X_test = mapper.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>9000</th>\n",
       "      <th>9001</th>\n",
       "      <th>9002</th>\n",
       "      <th>9003</th>\n",
       "      <th>9004</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>feat_1</th>\n",
       "      <td>629.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>795.0</td>\n",
       "      <td>731.0</td>\n",
       "      <td>526.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>969.0</td>\n",
       "      <td>1009.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>434.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>439.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_4</th>\n",
       "      <td>4.0</td>\n",
       "      <td>246.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5</th>\n",
       "      <td>NaN</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6</th>\n",
       "      <td>str_0.0</td>\n",
       "      <td>str_2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_5.0</td>\n",
       "      <td>str_1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_7</th>\n",
       "      <td>NaN</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_0.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8</th>\n",
       "      <td>str_1.0</td>\n",
       "      <td>str_4.0</td>\n",
       "      <td>str_5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>str_2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           9000     9001     9002     9003     9004\n",
       "feat_1    629.0      NaN    795.0    731.0    526.0\n",
       "feat_2      NaN      NaN      NaN    969.0   1009.0\n",
       "feat_3      NaN      NaN    434.0      NaN    439.0\n",
       "feat_4      4.0    246.0      NaN     -7.0      NaN\n",
       "feat_5      NaN  str_1.0  str_1.0  str_1.0      NaN\n",
       "feat_6  str_0.0  str_2.0      NaN  str_5.0  str_1.0\n",
       "feat_7      NaN  str_1.0  str_1.0  str_0.0      NaN\n",
       "feat_8  str_1.0  str_4.0  str_5.0      NaN  str_2.0"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test[numerical_features + categorical_features].head().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>9000</th>\n",
       "      <th>9001</th>\n",
       "      <th>9002</th>\n",
       "      <th>9003</th>\n",
       "      <th>9004</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>feat_1</th>\n",
       "      <td>-8.120688e-02</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.811367e+00</td>\n",
       "      <td>1.081700</td>\n",
       "      <td>-1.255515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_2</th>\n",
       "      <td>4.815402e-15</td>\n",
       "      <td>4.815402e-15</td>\n",
       "      <td>4.815402e-15</td>\n",
       "      <td>-0.649771</td>\n",
       "      <td>1.044498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_3</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>-5.055161e-01</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.367879</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_4</th>\n",
       "      <td>-9.907284e-01</td>\n",
       "      <td>2.376303e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>-1.143775</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_UNK</th>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_0.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_1.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_2.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_3.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_4.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_5.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_6.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_7.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_5_x0_str_8.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6_x0_UNK</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6_x0_str_0.0</th>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6_x0_str_1.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6_x0_str_2.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6_x0_str_3.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6_x0_str_4.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_6_x0_str_5.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_7_x0_UNK</th>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_7_x0_str_0.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_7_x0_str_1.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_7_x0_str_2.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_7_x0_str_3.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_UNK</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_0.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_1.0</th>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_2.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_3.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_4.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_5.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_6.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>feat_8_x0_str_7.0</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           9000          9001          9002      9003  \\\n",
       "feat_1            -8.120688e-02  0.000000e+00  1.811367e+00  1.081700   \n",
       "feat_2             4.815402e-15  4.815402e-15  4.815402e-15 -0.649771   \n",
       "feat_3             0.000000e+00  0.000000e+00 -5.055161e-01  0.000000   \n",
       "feat_4            -9.907284e-01  2.376303e+00  0.000000e+00 -1.143775   \n",
       "feat_5_x0_UNK      1.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_0.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_1.0  0.000000e+00  1.000000e+00  1.000000e+00  1.000000   \n",
       "feat_5_x0_str_2.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_3.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_4.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_5.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_6.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_7.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_5_x0_str_8.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_6_x0_UNK      0.000000e+00  0.000000e+00  1.000000e+00  0.000000   \n",
       "feat_6_x0_str_0.0  1.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_6_x0_str_1.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_6_x0_str_2.0  0.000000e+00  1.000000e+00  0.000000e+00  0.000000   \n",
       "feat_6_x0_str_3.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_6_x0_str_4.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_6_x0_str_5.0  0.000000e+00  0.000000e+00  0.000000e+00  1.000000   \n",
       "feat_7_x0_UNK      1.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_7_x0_str_0.0  0.000000e+00  0.000000e+00  0.000000e+00  1.000000   \n",
       "feat_7_x0_str_1.0  0.000000e+00  1.000000e+00  1.000000e+00  0.000000   \n",
       "feat_7_x0_str_2.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_7_x0_str_3.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_8_x0_UNK      0.000000e+00  0.000000e+00  0.000000e+00  1.000000   \n",
       "feat_8_x0_str_0.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_8_x0_str_1.0  1.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_8_x0_str_2.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_8_x0_str_3.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_8_x0_str_4.0  0.000000e+00  1.000000e+00  0.000000e+00  0.000000   \n",
       "feat_8_x0_str_5.0  0.000000e+00  0.000000e+00  1.000000e+00  0.000000   \n",
       "feat_8_x0_str_6.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "feat_8_x0_str_7.0  0.000000e+00  0.000000e+00  0.000000e+00  0.000000   \n",
       "\n",
       "                       9004  \n",
       "feat_1            -1.255515  \n",
       "feat_2             1.044498  \n",
       "feat_3            -0.367879  \n",
       "feat_4             0.000000  \n",
       "feat_5_x0_UNK      1.000000  \n",
       "feat_5_x0_str_0.0  0.000000  \n",
       "feat_5_x0_str_1.0  0.000000  \n",
       "feat_5_x0_str_2.0  0.000000  \n",
       "feat_5_x0_str_3.0  0.000000  \n",
       "feat_5_x0_str_4.0  0.000000  \n",
       "feat_5_x0_str_5.0  0.000000  \n",
       "feat_5_x0_str_6.0  0.000000  \n",
       "feat_5_x0_str_7.0  0.000000  \n",
       "feat_5_x0_str_8.0  0.000000  \n",
       "feat_6_x0_UNK      0.000000  \n",
       "feat_6_x0_str_0.0  0.000000  \n",
       "feat_6_x0_str_1.0  1.000000  \n",
       "feat_6_x0_str_2.0  0.000000  \n",
       "feat_6_x0_str_3.0  0.000000  \n",
       "feat_6_x0_str_4.0  0.000000  \n",
       "feat_6_x0_str_5.0  0.000000  \n",
       "feat_7_x0_UNK      1.000000  \n",
       "feat_7_x0_str_0.0  0.000000  \n",
       "feat_7_x0_str_1.0  0.000000  \n",
       "feat_7_x0_str_2.0  0.000000  \n",
       "feat_7_x0_str_3.0  0.000000  \n",
       "feat_8_x0_UNK      0.000000  \n",
       "feat_8_x0_str_0.0  0.000000  \n",
       "feat_8_x0_str_1.0  0.000000  \n",
       "feat_8_x0_str_2.0  1.000000  \n",
       "feat_8_x0_str_3.0  0.000000  \n",
       "feat_8_x0_str_4.0  0.000000  \n",
       "feat_8_x0_str_5.0  0.000000  \n",
       "feat_8_x0_str_6.0  0.000000  \n",
       "feat_8_x0_str_7.0  0.000000  "
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocessed_X_test.head().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'auc': 0.8813778495237811}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluation(pipeline, X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'auc': 0.8673832539797047}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluation(pipeline, X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
